package content

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.overscroll
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import cn.hutool.system.oshi.OshiUtil
import theme.divider

/**
 * @author zooy
 * @create 2024-01-12 1:50
 * @description
 */
@ExperimentalUnitApi
@Composable
fun CronContent() {

    Column(
        modifier = Modifier
            .fillMaxHeight()
            .fillMaxWidth()
            .verticalScroll(rememberScrollState())
        ,
    ) {
        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Start) {
            // 绘制Radio
            val checked = remember { mutableStateOf(false) }
            RadioButton(checked.value, onClick = {checked.value = !checked.value},
                colors = RadioButtonDefaults.colors(selectedColor = Color.White, unselectedColor = Color.White))
            MiniText("每秒 允许的通配符[, - * /]")
        }

        Divider(color = divider)

        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically) {
            // 绘制Radio
            val checked = remember { mutableStateOf(false) }
            RadioButton(checked.value, onClick = {checked.value = !checked.value},
                colors = RadioButtonDefaults.colors(selectedColor = Color.White, unselectedColor = Color.White))
            MiniText("周期从")
            val secondStart = remember { mutableStateOf(1) }
            MiniTextNumberField(secondStart)
            MiniText("到")
            val secondEnd = remember { mutableStateOf(20) }
            MiniTextNumberField(secondEnd)
            MiniText("秒")
        }

        Divider(color = divider)

        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Start) {
            // 绘制Radio
            val checked = remember { mutableStateOf(false) }
            RadioButton(checked.value, onClick = {checked.value = !checked.value},
                colors = RadioButtonDefaults.colors(selectedColor = Color.White, unselectedColor = Color.White))
            MiniText("指定")

            Column {
                repeat(6) { index ->
                    Row(
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        for (i in 0 until 10) {
                            Checkbox(checked = true, onCheckedChange = {}, colors = CheckboxDefaults.colors(checkedColor = Color.White))
                            MiniText((index * 10 + i).toString())
                        }
                    }
                }
            }
        }

        Divider(color = divider)

        Row(modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Start,
            verticalAlignment = Alignment.CenterVertically) {

            val cronText = remember { mutableStateOf("") }
            MiniText("CRON表达式: ")
            MiniTextField(cronText)
        }

    }
}

@ExperimentalUnitApi
@Composable
fun MiniTextNumberField(
    text: MutableState<Int>
) {
    BasicTextField(
        modifier = Modifier
            .width(40.dp)
            .height(36.dp)
            .border(1.dp, Color.White)
            .padding(0.dp)
            .wrapContentSize(Alignment.Center)
        ,
        value = text.value.toString(),
        onValueChange = {
            if (it.isNullOrEmpty()) {
                text.value = 0
            } else {
                if (it.toInt() > 59) {
                    text.value = 59
                } else {
                    text.value = it.toInt()
                }
            }
        },
        textStyle = MaterialTheme.typography.body2.copy(
            fontWeight = FontWeight.ExtraLight,
            color = Color.White,
            textAlign = TextAlign.Center,
            lineHeight = TextUnit(2.0f, TextUnitType.Em)
        )
    )
}

@ExperimentalUnitApi
@Composable
fun MiniTextField(
    text: MutableState<String>
) {
    BasicTextField(
        modifier = Modifier
            .fillMaxWidth()
            .height(36.dp)
            .border(1.dp, Color.White)
            .padding(0.dp)
            .wrapContentSize(Alignment.Center)
        ,
        value = text.value,
        onValueChange = {
            text.value = it
        },
        textStyle = MaterialTheme.typography.body2.copy(
            fontWeight = FontWeight.ExtraLight,
            color = Color.White,
            textAlign = TextAlign.Center,
            lineHeight = TextUnit(1.0f, TextUnitType.Em)
        )
    )
}

@OptIn(ExperimentalUnitApi::class)
@Composable
fun MiniText(
    value: String
) {
    Box(modifier = Modifier.height(48.dp).wrapContentWidth(), contentAlignment = Alignment.CenterStart) {
        Text(
            text = value,
            modifier = Modifier.wrapContentSize(Alignment.Center),
            color = Color.White,
            style = MaterialTheme.typography.body2.copy(
                fontWeight = FontWeight.ExtraLight
            ),
            textAlign = TextAlign.Center,
            lineHeight = TextUnit(1.0f, TextUnitType.Em)
        )
    }
}
